package com.zxiw.zxedu.recyclerview.manager

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zxiw.zxedu.recyclerview.adapter.HelperStateRecyclerViewAdapter
import com.zxiw.zxedu.recyclerview.group.GroupedStateRecyclerViewAdapter

/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description
 * 描述：自定义GridLayoutManager</p>
 * 主要用于，如果你是GridLayoutManager布局有3列，但是要显示空页面的时候需要1列，也就是整个一个地方都是空白<br/>
 */
class StateGridLayoutManager(
    context: Context,
    spanCount: Int,
    orientation: Int = RecyclerView.VERTICAL,
    reverseLayout: Boolean = false
) :
    GridLayoutManager(context, DEFAULT_SPAN_SIZE, orientation, reverseLayout) {
    var mSpanCount: Int = 0

    init {
        this.mSpanCount = spanCount
    }

    companion object {
        private const val DEFAULT_SPAN_SIZE = 1
    }


    override fun onItemsChanged(recyclerView: RecyclerView) {
        val adapter = recyclerView.adapter
        var adapterState = false
        if (adapter is HelperStateRecyclerViewAdapter<*>) {
            val state = adapter.state
            if (state == HelperStateRecyclerViewAdapter.STATE_NORMAL) {
                adapterState = true
            }
        } else if (adapter is GroupedStateRecyclerViewAdapter<*>) {
            val state = adapter.state
            if (state == HelperStateRecyclerViewAdapter.STATE_NORMAL) {
                adapterState = true
            }
        }
        val span =
            if (itemCount == DEFAULT_SPAN_SIZE) if (adapterState) mSpanCount else DEFAULT_SPAN_SIZE else mSpanCount
        spanCount = span
        super.onItemsChanged(recyclerView)
    }
}